package com.zengjianjun.push.msg.platform.wecom;

import cn.hutool.core.text.StrPool;
import com.alibaba.fastjson2.JSON;
import com.zengjianjun.push.msg.enums.PushMsgPlatformEnum;
import com.zengjianjun.push.msg.enums.PushMsgTypeEnum;
import com.zengjianjun.push.msg.exception.PushMsgException;
import com.zengjianjun.push.msg.platform.AbstractPushMsgClient;
import com.zengjianjun.push.msg.platform.param.BasePushMsgParam;
import com.zengjianjun.push.msg.platform.wecom.param.MarkdownMsgParam;
import com.zengjianjun.push.msg.platform.wecom.param.TextMsgParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zengjianjun
 */
@Slf4j
@Component
public class WeComClient extends AbstractPushMsgClient {

    /**
     * 字符常量: #
     */
    private static final String POUND = "#";

    @Override
    public PushMsgPlatformEnum get() {
        return PushMsgPlatformEnum.WeCom;
    }

    @Override
    protected String convertBody(BasePushMsgParam param) {
        PushMsgTypeEnum msgTypeEnum = param.getPushMsgTypeEnum();
        if (msgTypeEnum == null) {
            throw PushMsgException.build("消息类型不能为空");
        }
        List<String> mentionedList = new ArrayList<>();
        List<String> mentionedMobileList = new ArrayList<>();
        if (param.isNoticeAll()) {
            mentionedList.add("@all");
        } else {
            if (!CollectionUtils.isEmpty(param.getNoticeUserList())) {
                mentionedList.addAll(param.getNoticeUserList());
            }
            if (!CollectionUtils.isEmpty(param.getNoticeMobileList())) {
                mentionedMobileList.addAll(param.getNoticeMobileList());
            }
        }
        StringBuilder content = new StringBuilder();
        switch (msgTypeEnum) {
            case TEXT:
                content.append(param.getTitle()).append(StrPool.LF)
                        .append("             ").append(StrPool.LF)
                        .append(param.getContent()).append(StrPool.LF);
                return JSON.toJSONString(TextMsgParam.builder().msgType(msgTypeEnum.getCode())
                        .text(TextMsgParam.Text.builder()
                                .content(content.toString())
                                .mentionedList(mentionedList)
                                .mentionedMobileList(mentionedMobileList)
                                .build())
                        .build());
            case MARKDOWN:
                content.append(POUND).append(" ").append(param.getTitle()).append(StrPool.LF)
                        .append("              ").append(StrPool.LF)
                        .append(param.getContent());
                return JSON.toJSONString(MarkdownMsgParam.builder().msgType(msgTypeEnum.getCode())
                        .markdown(MarkdownMsgParam.Markdown.builder()
                                .content(content.toString())
                                .build())
                        .build());
            default:
                throw PushMsgException.build("不支持的消息类型：{}", msgTypeEnum.getDesc());
        }
    }
}
